<?php

define('VS_MAIN', '');
//define('DEVEL', '');

ob_start();

//error_reporting(0);

error_reporting(E_ALL && E_NOTICE);
error_reporting(E_ERROR);
ini_set('display_errors', '1');

// Common Includes
require_once("../path_init.php");
require_once("lib/vslibdef.php");

require_once($dirConfig."config.php");

// Top Page Initialization
require_once(DIR_LIB."VsCore".DIR_SEP."vsapplication.php");
require_once(DIR_LIB."VsCore".DIR_SEP."db.php");
require_once(DIR_LIB."VsCore".DIR_SEP."vsauth.php");

$app = VsApplication::Instance();
$session = VsSession::Instance();
$lang = 'BG';
$currencie = $lang=='BG' ? 'лв.' : '$';
$session->assign('lang', $lang);
$session->assign('currencie', $currencie);

$app->setTitle("JUNONA - Заплати на Персонала");
$auth = VsAuth::Instance();

$db = VsDatabase::Instance();
if($db->error()) {
	echo $db->error();

	exit();
}


if(isset($_REQUEST['act']) && $_REQUEST['act']=='logout') {
	$auth->logout();

	header("Location: ".$_SERVER['PHP_SELF']);
}

// Only For Login Form
require_once("smarty_for_login.php");

function showLogin() {
	global $dirInclude;
	
	$formValueUser = '';
	$formErrorUser = '';
	$formErrorPass = '';
	
	require_once(DIR_LIB."VsCore".DIR_SEP."templates".DIR_SEP."login.php");

	$form = new VsLoginForm;
	echo $form->show();
}
//======================================================

if(!defined('DEVEL') && !$auth->isAuth()) {
	if(isset($_POST['sublogin'])) {
		if($authError = $auth->setAuth($_POST['user'], $_POST['pass'])) {

			if($authError==1)
				$formError = "Грешен потребител или парола !!!";
			else if($authError==2)
				$formError = "Грешен потребител или парола !!!";
			else
				$formError = "Грешен потребител или парола !!!";
			
			showLogin();
		}
		
		unset($_POST['sublogin']);
	}
	else
		showLogin();

	if(!$auth->isAuth())
		exit();
}

/*
 * If the request is ajax , desable  page layout.	
 */
if(strpos($_SERVER['PHP_SELF'], 'json_models.php') === FALSE) {
	require_once("templates/scbpage.php");
	$page = ScbPage::Instance();

	if($auth->level()==9) {
		require_once(dirname(__FILE__).DIR_SEP."templates".DIR_SEP."XCart".DIR_SEP."vsadmin_toolbar.php");
	
		$page->setAdminToolbar(new VsAdminToolbar());
	}
}
?>
